var app = getApp();
Page({
  data: {
    activeIndex: 0,
    navList: [],
    order: { 
      items: [],
      paginate:{ } 
    },
    prompt: {
      hidden: !0,
      icon: '../../../assets/images/iconfont-order-default.png',
      title: '您还没有相关的报名信息',
      text: '可以去看看有没有想要报名的补习班',
      gurl:'../../../pages/home/home',
      gtype:"switchTab"
    },
    page:1,
    limit:10,
    dtype:"all"
  },
  onLoad() {
    this.setData({
      navList: [
        {
          name: '全部',
          _id: 'all',
        },
        {
          name: '未审核',
          _id: 'wsh',
        },
        {
          name: '通过',
          _id: 'shtg',
        },
        {
          name: '未通过',
          _id: 'shwtg',
        }, 
        {
          name: '已付款',
          _id: 'dpj',
        },
        {
          name: '已取消',
          _id: 'yqx',
        },
      ]
    })
    var that = this;
    var openid = wx.getStorageSync('openid');
    var sid = wx.getStorageSync('sid');
    if (openid == "" || sid == "" ) {
      wx.redirectTo({
        url: '../../pages/index/index'
      })
    } else {
      that.getdata();
    }
  },

  getdata: function () {//定义函数名称
    var that = this;   
    wx.showLoading({
      title: '加载中',
    })
    var sid = wx.getStorageSync('sid');
    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/orderList.html',//请求地址
      data: {//发送给后台的数据
        page: that.data.page,
        limit: that.data.limit,
        dtype: that.data.dtype,
        sid: sid
      },
      header: {//请求头
        "Content-Type": "applciation/json"
      },
      method: "GET",//get为默认方法/POST
      success: function (res) {
        wx.hideLoading()
        //报名列表
        var orderarr = that.data.order.items;
        var order = res.data.items;
        for (var i = 0; i < order.length;i++){
          orderarr.push(order[i]);
        }
        var paginate = res.data.paginate;
        var key1 ="order.items";
        var key2 = "order.paginate";
        var obj = {};
        obj[key1] = orderarr;
        obj[key2] = paginate;
        that.setData(obj);
        that.setData({
          'prompt.hidden': orderarr.length
        });
        
      },
      fail: function (err) { },//请求失败
      complete: function () { }//请求完成后执行的函数
    })
  },

  //上拉刷新
  tolower() {
    var that = this;
    var page = parseInt(that.data.page) + 1;
    that.setData({
      page: page
    });
    that.getdata();
  },

  //切换
  onTapTag(e) {
    var that = this;
    var dtype = e.currentTarget.dataset.type;
    var index = e.currentTarget.dataset.index;
    that.setData({
      activeIndex: index,
      dtype: dtype ,
      order: {
        items: [],
        paginate: {}
      },
      page: 1
    })
    that.getdata();
  },

  //删除报名
  delOrder(e) {
    wx.showLoading({
      title: '删除中...',
    })
    var oid = e.currentTarget.dataset.orderid;
    var that = this;  
    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/delOrder.html',//请求地址
      data: {
        oid: oid
      },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "GET",//get为默认方法/POST
      success: function (res) {
        that.setData({
          order: {
            items: [],
            paginate: {}
          },
          page: 1
        })
        that.getdata();
        wx.hideLoading()
      },
      fail: function (err) { },//请求失败
      complete: function () { }//请求完成后执行的函数
    })
  },


  //取消报名
  cancelOrder(e) {
    wx.showLoading({
      title: '删除中...',
    })
    var oid = e.currentTarget.dataset.orderid;
    var that = this;
    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/cancelOrder.html',//请求地址 
      data: {
        oid: oid
      },
      header: {
        "Content-Type": "applciation/json"
      },
      method: "GET",//get为默认方法/POST
      success: function (res) {
        that.setData({
          order: {
            items: [],
            paginate: {}
          },
          page: 1
        })
        that.getdata();
        wx.hideLoading()
      },
      fail: function (err) { },//请求失败
      complete: function () { }//请求完成后执行的函数
    })
  }


})